<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->

<template>
  <div>
    <header class="home-header wrap" :class="{ active: state.headerScroll }">
      <router-link tag="i" to="./category"
        ><i class="nbicon nbmenu2"></i
      ></router-link>
      <div class="header-search">
        <span class="app-name">金砖商贸</span>
        <i class="iconfont icon-search" style="padding-right: 3px;"></i>
        <!-- <router-link
          tag="span"
          class="search-title"
          to="./product-list?from=home"
          >山河无恙，人间皆安</router-link
        > -->
        <router-link
          tag="span"
          class="search-title"
          to="./product-list?from=home"
          >点击进入搜索页面<van-icon name="search"  style="padding-left: 12px;" /></router-link
        >
      </div>
      <router-link class="login" tag="span" to="./login" v-if="!state.isLogin"
        >登录</router-link
      >
      <router-link class="login" tag="span" to="./user" v-else>
        <van-icon name="manager-o" />
      </router-link>
    </header>
    <nav-bar />
    <!-- <swiper :list="state.swiperList"></swiper> -->
    <!-- <div class="category-list">
      <div v-for="item in state.categoryList" v-bind:key="item.categoryId" @click="tips">
        <img :src="item.imgUrl">
        <span>{{item.name}}</span>
      </div>
    </div> -->
    <div class="good" style="margin-top: 40px">
      <header class="good-header">热门推荐</header>
      <van-skeleton title :row="3" :loading="state.loading">
        <div class="good-box">
          <div
            class="good-item"
            v-for="item in state.newGoodses"
            :key="item.id"
            @click="goToDetail(item)"
          >
            <img :src="$filters.prefix(item.cover_img)" alt="" />
            <div class="good-desc">
              <div class="title">
                <span v-if="item.type + '' === '0'" style="font-weight: bold;">需求：</span>
                <span v-else-if="item.type + '' === '1'" style="font-weight: bold;">商品：</span>
                {{ item.title }}</div>
              <div v-if="item.salary_up" class="price">
                ¥ {{ item.salary_down }}~{{ item.salary_up }}
              </div>
              <div v-else class="price">
                ¥ {{ item.salary_down }}
              </div>
            </div>
          </div>

          <div class="more">
            <span @click="hotMore">加载更多...</span>
          </div>
        </div>
      </van-skeleton>
    </div>
    <div class="good" style="padding-bottom: 65px">
      <header class="good-header">商品列表</header>
      <van-skeleton title :row="3" :loading="state.loading">
        <div class="good-box">
          <div
            class="good-item"
            v-for="item in state.hots"
            :key="item.id"
            @click="goToDetail(item)"
          >
            <img :src="$filters.prefix(item.cover_img)" alt="" />
            <div class="good-desc">
              <div class="title">
                <span v-if="item.type + '' === '0'" style="font-weight: bold;">需求：</span>
                <span v-else-if="item.type + '' === '1'" style="font-weight: bold;">商品：</span>
                {{ item.title }}</div>
              <div v-if="item.salary_up" class="price">
                ¥ {{ item.salary_down }}~{{ item.salary_up }}
              </div>
              <div v-else class="price">
                ¥ {{ item.salary_down }}
              </div>
            </div>
          </div>

          <div class="more">
            <span @click="hotList">加载更多...</span>
          </div>
        </div>
      </van-skeleton>
    </div>
    <!-- <div class="good" :style="{ paddingBottom: '100px'}">
      <header class="good-header">最新推荐</header>
      <van-skeleton title :row="3" :loading="state.loading">
        <div class="good-box">
          <div class="good-item" v-for="item in state.recommends" :key="item.goodsId" @click="goToDetail(item)">
            <img :src="$filters.prefix(item.goodsCoverImg)" alt="">
            <div class="good-desc">
              <div class="title">{{ item.goodsName }}</div>
              <div class="price">¥ {{ item.sellingPrice }}</div>
            </div>
          </div>
        </div>
      </van-skeleton>
    </div> -->
  </div>
</template>

<script setup>
import { reactive, onMounted, nextTick } from "vue";
import { useRouter } from "vue-router";
import swiper from "@/components/Swiper.vue";
import navBar from "@/components/NavBar.vue";
import { getHome, getKeyword } from "@/service/home";
import { getUserInfo } from "@/service/user";
import { getLocal, setLocal } from "@/common/js/utils";
import { showLoadingToast, closeToast, showToast, showNotify } from "vant";
import { useCartStore } from "@/stores/cart";
const cart = useCartStore();
const router = useRouter();
const state = reactive({
  swiperList: [], // 轮播图列表
  isLogin: false, // 是否已登录
  headerScroll: false, // 滚动透明判断
  hots: [],
  newGoodses: [],
  recommends: [],
  categoryList: [],
  loading: true,
  hotQueryParams: {
    pageNum: 1,
    pageSize: 4,
    pages: 1,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 12,
  },
});
onMounted(async () => {
  const token = getLocal("access_token");
  if (token) {
    state.isLogin = true;
    // 获取购物车数据.
    // cart.updateCart()
    try {
      const res = await getUserInfo();
      setLocal("user", JSON.stringify(res));
    } catch (error) {
      localStorage.clear();
      showToast({
        message: "请先登录",
        className: "particulars-detail-popup",
      });
      setTimeout(() => {
        router.push({ path: `/login` });
      }, 0);
    }
  } else {
    showToast({
      message: "请先登录",
      className: "particulars-detail-popup",
    });
    setTimeout(() => {
      router.push({ path: `/login` });
    }, 0);
  }
  showLoadingToast({
    message: "加载中...",
    forbidClick: true,
  });

  getHots();

  getList();

  // const { data } = await getHome()
  // state.swiperList = data.carousels
  // state.newGoodses = data.newGoodses
  // state.hots = data.hotGoodses
  // state.recommends = data.recommendGoodses
  state.loading = false;
  closeToast();
});

nextTick(() => {
  document.body.addEventListener("scroll", () => {
    let scrollTop =
      window.pageYOffset ||
      document.documentElement.scrollTop ||
      document.body.scrollTop;
    scrollTop > 100
      ? (state.headerScroll = true)
      : (state.headerScroll = false);
  });
});

const goToDetail = (item) => {
  router.push({ path: `/product/${item.id}` });
};

const tips = () => {
  showToast("敬请期待");
};

// 获取热门推荐
const getHots = async () => {
  const res = await getKeyword(state.hotQueryParams);
  if (res && res.items.length > 0) {
    state.newGoodses.push(...res.items);
  } else {
    showNotify({
      message: "已经到底了！",
      color: "#666",
      background: "#fff",
    });
  }
};

// 热门推荐翻页
const hotMore = async () => {
  state.hotQueryParams.pageNum += 1;
  getHots();
};

const getList = async () => {
  const res = await getKeyword(state.queryParams);
  if (res && res.items.length > 0) {
    state.hots.push(...res.items);
  } else {
    showNotify({
      message: "已经到底了！",
      color: "#666",
      background: "#fff",
    });
  }
};

const hotList = async () => {
  state.queryParams.pageNum += 1;
  getList();
};
</script>

<style lang="less" scoped>
@import "../common/style/mixin";
.home-header {
  position: fixed;
  left: 0;
  top: 0;
  .wh(100%, 50px);
  .fj();
  line-height: 50px;
  padding: 0 15px;
  .boxSizing();
  font-size: 15px;
  color: #fff;
  z-index: 10000;
  .nbmenu2 {
    color: @primary;
  }
  &.active {
    background: @primary;
    .nbmenu2 {
      color: #fff;
    }
    .login {
      color: #fff;
    }
  }

  .header-search {
    display: flex;
    width: 74%;
    line-height: 20px;
    margin: 10px 0;
    padding: 5px 0;
    color: #232326;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 20px;
    .app-name {
      padding: 0 10px;
      color: @primary;
      font-size: 20px;
      font-weight: bold;
      border-right: 1px solid #666;
    }
    .icon-search {
      padding: 0 10px;
      font-size: 17px;
    }
    .search-title {
      font-size: 12px;
      color: #666;
      line-height: 21px;
    }
  }
  .icon-iconyonghu {
    color: #fff;
    font-size: 22px;
  }
  .login {
    color: @primary;
    line-height: 52px;
    .van-icon-manager-o {
      font-size: 20px;
      vertical-align: -3px;
    }
  }
}
.category-list {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 13px;
  div {
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;
    img {
      .wh(36px, 36px);
      margin: 13px auto 8px auto;
    }
  }
}
.good {
  .good-header {
    background: #f9f9f9;
    height: 50px;
    line-height: 50px;
    text-align: center;
    color: @primary;
    font-size: 16px;
    font-weight: 500;
  }
  .good-box {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    .good-item {
      box-sizing: border-box;
      width: 50%;
      border-bottom: 1px solid #e9e9e9;
      padding: 10px 10px;
      img {
        display: block;
        width: 120px;
        height: 100px;
        margin: 0 auto;
      }
      .good-desc {
        text-align: center;
        font-size: 14px;
        padding: 10px 0;
        .title {
          color: #222333;
        }
        .price {
          color: @primary;
        }
      }
      &:nth-child(2n + 1) {
        border-right: 1px solid #e9e9e9;
      }
    }
  }

  .more {
    background: #f9f9f9;
    width: 100%;
    display: block;
    text-align: center;
    padding: 15px 0;
  }
}
.floor-list {
  width: 100%;
  padding-bottom: 50px;
  .floor-head {
    width: 100%;
    height: 40px;
    background: #f6f6f6;
  }
  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    .boxSizing();
    .floor-category {
      width: 50%;
      padding: 10px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      .boxSizing();
      &:nth-child(2n) {
        border-right: none;
      }
      p {
        font-size: 17px;
        color: #333;
        &:nth-child(2) {
          padding: 5px 0;
          font-size: 13px;
          color: @primary;
        }
      }
      .floor-products {
        .fj();
        width: 100%;
        img {
          .wh(65px, 65px);
        }
      }
    }
  }
}
</style>
